<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>客户管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/resources/lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="/resources/lib/font-awesome-4.7.0/css/font-awesome.min.css" media="all">
</head>
<body>
<!-- 查询条件开始 -->
<blockquote class="layui-elem-quote">
    <form class="layui-form" id="searchFrm" lay-filter="searchFrm">
        <div class="layui-form-item">
            <div class="layui-inline" id="userIdSearch">
                <label class="layui-form-label">业务员</label>
                <div class="layui-input-inline">
                    <select name="userId" id="searchUserId">
                        <option value="">请选择业务员</option>
                    </select>
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">客户姓名</label>
                <div class="layui-input-inline">
                    <input type="text" name="name" placeholder="请输入客户姓名" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">客户电话</label>
                <div class="layui-input-inline">
                    <input type="text" name="phone" placeholder="请输入客户电话" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <input type="button" value="查询" class="layui-btn" lay-submit id="doSearch" lay-filter="doSearch">
                <input type="reset" value="重置" class="layui-btn layui-btn-danger">
            </div>
        </div>
    </form>
</blockquote>
<!-- 查询条件结束-->
<!-- 数据表格开始 -->
<div>
    <table class="layui-hide" id="customerTable" lay-filter="customerTable"></table>
    <script type="text/html" id="customerTableToolbar">
        <button class="layui-btn layui-btn-sm" lay-event="add">添加</button>
        <button class="layui-btn layui-btn-sm layui-btn-danger" id="changeUserBtn" lay-event="changeUser">转移客户</button>
    </script>
    <script type="text/html" id="customerTableBar">
        <a class="layui-btn layui-btn-xs" lay-event="update">修改</a>
        <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="del">删除</a>
        <a class="layui-btn layui-btn-xs" lay-event="viewVisitLog">查看拜访记录</a>
    </script>
</div>
<!-- 数据表格结束-->


<!-- 弹出层开始 -->
<div  id="addOrUpdateForm" style="display: none;margin: 5px">
    <form class="layui-form" id="dataFrm" lay-filter="dataFrm">
        <div class="layui-form-item">
            <label class="layui-form-label">客户姓名</label>
            <div class="layui-input-block">
                <input type="hidden" name="id">
                <input type="text" name="name" lay-verify="required" placeholder="请输入客户姓名" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">客户电话</label>
            <div class="layui-input-block">
                <input type="text" name="phone" lay-verify="required" placeholder="请输入客户电话" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">客户公司</label>
            <div class="layui-input-block">
                <input type="text" name="company" lay-verify="required" placeholder="请输入客户公司" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">性别</label>
            <div class="layui-input-inline">
                <input type="radio" name="sex" checked value="1" title="男">
                <input type="radio" name="sex"  value="0" title="女">
            </div>
        </div>
        <div class="layui-form-item" style="text-align: center">
            <input type="button" value="提交" class="layui-btn" lay-submit id="doSubmit" lay-filter="doSubmit">
            <input type="reset" value="重置" id="doReset" class="layui-btn layui-btn-danger">
        </div>
    </form>
</div>
<!-- 弹出层结束-->


<!-- 转移客户弹出层开始 -->
<div id="changeCustomerUserForm" style="display: none;margin: 5px" >
    <form class="layui-form" id="changeUserFrm" lay-filter="changeUserFrm">
        <div class="layui-form-item">
            <label class="layui-form-label" style="width: 100px">请选择业务员</label>
            <div class="layui-input-block" id="userRadioDiv">

            </div>
        </div>
        <div class="layui-form-item" style="text-align: center">
            <input type="button" value="确定" class="layui-btn" lay-submit id="doChange" lay-filter="doChange">
        </div>
    </form>

</div>
<!-- 转移客户弹出层结束 -->


<!-- 客户拜访记录弹出层开始 -->
<div id="customerVisitLogDiv" style="display: none;margin: 5px">
    <div class="layui-panel" style="padding: 2px">
        <form class="layui-form" id="visitLogFrm" lay-filter="visitLogFrm">
            <div class="layui-form-item">
                <label class="layui-form-label">拜访时间</label>
                <div class="layui-input-block">
                    <input type="text" class="layui-input" lay-verify="required" readonly id="visitTime" name="visitTime" placeholder="请选择拜访时间">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">拜访内容</label>
                <div class="layui-input-block">
                    <textarea class="layui-textarea" lay-verify="required" name="descp"></textarea>
                </div>
            </div>
            <div class="layui-form-item" style="text-align: center">
                <input type="button" value="保存" class="layui-btn" lay-submit id="doVisitLogSubmit" lay-filter="doVisitLogSubmit">
            </div>
        </form>
    </div>
    <div class="layui-panel">
        <table class="layui-hide" id="visitLogTable" lay-filter="visitLogTable"></table>
    </div>
</div>
<!-- 客户拜访记录弹出层结束 -->



</body>
<script src="/resources/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script src="/resources/js/lay-config.js?v=1.0.4" charset="utf-8"></script>
<script>
    layui.use(['jquery','table','layer','form','laydate'],function () {
        let $=layui.jquery;
        let table=layui.table;
        let layer=layui.layer;
        let form=layui.form;
        let laydate=layui.laydate;

        //请求查询条件里面的业务员
        $.get("/user/queryAllAvailableUser.do",function (res) {
            let data=res.data;
            if(data){
                let html='<option value="">请选择业务员</option>';
                $.each(data,function (i,item) {
                    html+='<option value="'+item.id+'">'+item.realname+'</option>';
                })
                //放到searchUserId
                $("#searchUserId").html(html)
                console.log($("#searchUserId").html())
                //重新渲染页面所有下拉框
                form.render('select')
            }
        })

        //渲染数据表格
        let customerTable=table.render({
            elem: '#customerTable'
            ,url:'/customer/list.do'
            ,toolbar: '#customerTableToolbar' //开启头部工具栏，并为其绑定左侧模板
            ,defaultToolbar: ['filter', 'exports', 'print']
            ,title: '客户数据表' //导出excel的文件名
            ,cellMinWidth:140
            ,cols: [ [
                {type:"checkbox",fixed: "left"}
                ,{field:'id', title:'ID',align:"center",fixed: "left"}
                ,{field:'name', title:'客户姓名',align:"center"}
                ,{field:'phone', title:'电话',align:"center"}
                ,{field:'realname', title:'所属业务员',align:"center"}
                ,{field:'sex', title:'性别',align:"center",   templet: function(res){
                        if(res.sex==1){
                            return "<font color='blue'>男</font>"
                        }else if(res.sex==0){
                            return "<font color='green'>女</font>"
                        }
                        return '未知'
                    }}
                ,{field:'company', title:'公司',align:"center"}
                ,{field:'createTime', title:'创建时间',align:"center",width: 180}
                ,{field:'updateTime', title:'更新时间',align:"center",width: 180}
                ,{field:'deletedTime', title:'离职时间',align:"center",width: 180}
                ,{fixed: 'right', title:'操作', toolbar: '#customerTableBar',width:'260',align:"center"}
            ] ]
            ,page: true
            ,done:function (){
                //判断用户的角色
                let role=[[${session.user.role}]];
                if(role==2){//业务员
                    $("#userIdSearch").hide();
                    console.log($("#changeUserBtn").length)
                    $("#changeUserBtn").hide();
                }
            }
        });

        //监听模糊查询
        form.on("submit(doSearch)",function (obj) {
            console.log(obj);
            let data=obj.field;
            //刷新表格把数据传过去
            customerTable.reload({
                where:data
            })
            return false;
        })
        //监听表头的事件
        table.on("toolbar(customerTable)",function (obj) {
            console.log(obj);
            switch (obj.event){
                case 'add':
                    openAddLayer();//打开添加的弹出层
                    break;
                case 'changeUser':
                    openChangeUserLayer();
                    break;
            }
        });
        //监听行工具条的事件
        table.on('tool(customerTable)', function(obj){
            switch (obj.event){
                case 'update':
                    openUpdateLayer(obj.data)
                    break;
                case 'del':
                    layer.confirm("你确定要删除【"+obj.data.name+"】这个客户吗?",{icon: 3, title:'提示'},function () {
                        $.post("/customer/del.do",{id:obj.data.id},function (res) {
                            layer.msg(res.msg);
                            if(res.code==200){
                                customerTable.reload();
                            }
                        })
                    })
                    break;
                case 'viewVisitLog':
                    openViewVisitLog(obj.data);
                    break;
            }
        });

        //打开添加的弹出层的方法
        let url; //提交的URL
        let mainIndex; //当前弹出层的index
        function openAddLayer() {
            mainIndex=layer.open({
                type: 1,//页面层
                title:'添加客户',
                content:$("#addOrUpdateForm"),
                area:['400px','400px'],
                success:function () {
                    $("#doReset").click();
                    url="/customer/addCustomer.do";
                }
            })
        }

        //打开修改的弹出层的方法
        function openUpdateLayer(data) {
            mainIndex=layer.open({
                type: 1,//页面层
                title:'修改['+data.name+']客户信息',
                content:$("#addOrUpdateForm"),
                area:['400px','400px'],
                success:function (){
                    //回显数据
                    form.val('dataFrm',data);
                    url="/customer/updateCustomer.do";
                }
            })
        }
        //监听提交事件
        form.on("submit(doSubmit)",function (obj) {
            let data=obj.field;
            //提交
            $.post(url,data,function (res) {
                layer.msg(res.msg);
                if(res.code==200){
                    //刷新表格
                    customerTable.reload();
                    //关闭弹出层
                    layer.close(mainIndex);
                }
            })
            return false;
        })

        //打开转移客户的弹出层
        function openChangeUserLayer(){
            //得到表格的选中行数据
            let checkStatus = table.checkStatus('customerTable'); //idTest 即为基础参数 id 对应的值
            let data=checkStatus.data;
            if(data.length<=0){
                layer.msg("请选中要转移的客户数据")
                return;
            }
            //打开弹出层
            mainIndex=layer.open({
                type: 1,//页面层
                title:'转移选中客户',
                content:$("#changeCustomerUserForm"),
                area:['600px','300px'],
                success:function (){
                    $.get("/user/queryAllAvailableUser.do",function (res) {
                        let data=res.data;
                        if(data){
                            let html="";
                            $.each(data,function (i,item) {
                                html+='<input type="radio" name="userId" value="'+item.id+'" title="'+item.realname+'" />';
                            })
                            //放到searchUserId
                            $("#userRadioDiv").html(html)
                            //重新渲染页面所有radio
                            form.render('radio')
                        }
                    })
                }
            })
        }
        //监听转移客户弹出层里面的提交按钮
        form.on("submit(doChange)",function (obj) {
            let userId=obj.field.userId;
            if(userId){
                //得到表格的选中行数据
                let checkStatus = table.checkStatus('customerTable'); //idTest 即为基础参数 id 对应的值
                let data=checkStatus.data;
                //声明一个提交到后台的数据对象  user.do?userId=1&customerIds=1&customerIds=2&customerIds=3
                let params="userId="+userId;
                $.each(data,function (index,item) {
                    params+="&customerIds="+item.id;
                })
                //向后台发请求
                $.post("/customer/changeCustomerUser.do",params,function (res) {
                    layer.msg(res.msg)
                    if(res.code==200){
                        customerTable.reload();
                        layer.close(mainIndex)
                    }
                })
            }else{
                layer.msg("请选择一个要转移的的业务员")
            }
            return false;
        })


        //打开拜访记录的弹出层
        function openViewVisitLog(data) {
            mainIndex=layer.open({
                type: 1,//页面层
                title:'添加【'+data.name+'】客户的拜访数据',
                content:$("#customerVisitLogDiv"),
                area:['800px','600px'],
                success:function (){
                    //绑定时间选择器
                    laydate.render({
                        elem:"#visitTime",
                        type:"datetime"
                    })
                    //渲染拜访数据表格  根据客户ID分页查询拜访数据
                    let visitLogTable=table.render({
                        elem: '#visitLogTable'
                        ,url:'/visitLog/listVisitLogByCustomerId.do?customerId='+data.id
                        ,defaultToolbar: ['filter', 'exports', 'print']
                        ,cellMinWidth:140
                        ,cols: [ [
                            {field:'realname', title:'业务员',align:"center"}
                            ,{field:'name', title:'客户姓名',align:"center"}
                            ,{field:'descp', title:'拜访内容',align:"center"}
                            ,{field:'visitTime', title:'拜访时间',align:"center",width: 180}
                            ,{field:'createTime', title:'创建时间',align:"center",width: 180}
                        ] ]
                        ,page: true
                    });

                    //给保存按钮添加事件
                    form.on("submit(doVisitLogSubmit)",function (obj) {
                        let params=obj.field;//json对象{"visitTime":'2020-11-11 11:11:11',"descp":"123412312"};
                        //向params里面添加客户ID和客户名称的属性
                        params.customerId=data.id;
                        params.name=data.name;
                        console.log(params);
                        //保存
                        $.post("/visitLog/addVisitLog.do",params,function (res) {
                            layer.msg(res.msg);
                            if(res.code==200){
                                visitLogTable.reload();//刷新拜访记录表格
                                //清空表单
                                $("#visitLogFrm")[0].reset();//把jq对象转成原生的dom对象
                                // document.getElementById("visitLogFrm").reset();
                            }
                        })
                        //阻止表单同步提交
                        return false;
                    })
                }
            })
        }
    })
</script>
</html>
